<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="java.sql.*" %>

<html>
	<head>
    <title>Trivial JDBC Example!</title>
	</head>
 
	<body>    
		<h4>Trivial JDBC Example!</h4> 	
		<table border='1'>

	
		  <%
		  Class.forName("com.mysql.jdbc.Driver"); 
		  String url = "jdbc:mysql://ece1779db.cumlmfbht0fr.us-east-1.rds.amazonaws.com/ece1779group5?" + "user=group5&password=6633715403";
	      
	      //String url = "jdbc:mysql://ece1779db.cf2zhhwzx2tf.us-east-1.rds.amazonaws.com/ece1779?" + "user=ece1779&password=Sp2012";
	      //url = "jdbc:mysql://localhost/ece1779?" + "user=ece1779&password=spring2012";
	      Connection con = DriverManager.getConnection(url);
	
	      Statement stmt = con.createStatement();
	
	      //String insert = "INSERT INTO users (id, login, password) VALUES (?,?,?);";
	      //PreparedStatement ps = con.prepareStatement(insert);
	      //ps.setInt(1,4);
	      //ps.setString(2,"rohan");
	      //ps.setString(3,"bali");	
	      //int rowsAffected = ps.executeUpdate();
	
	      //stmt.executeUpdate("INSERT INTO users (login, password) VALUES ('blah', 'bloh');");
	      //stmt.executeUpdate("INSERT INTO users VALUES ('1','gary', 'chaw');");
	       
	      String insert = "INSERT INTO images (userid, key1, key2, key3, key4) VALUES (?,?,?,?,?);";
		    
		  PreparedStatement ps = con.prepareStatement(insert);
		  // image id auto increments when inserting
		  ps.setInt(1, 1);
		  ps.setString(2, "blah");
		  ps.setString(3, "bloh");
		  ps.setString(4, "bleh");
		  ps.setString(5, "blih");
	      ps.executeUpdate();

	      String query="select * from images";
	    
	      ResultSet rs = stmt.executeQuery(query);
	      
	      while(rs.next()) { 
	      %>
			<tr>
				<td><%= rs.getInt("id") %></td>
				<td><%= rs.getString("userId") %></td>
				<td><%= rs.getString("key1") %></td>
				<td><%= rs.getString("key2") %></td>
				<td><%= rs.getString("key3") %></td>
				<td><%= rs.getString("key4") %></td>
			</tr>
	      
	      <%
	      }
	      rs.close();
	      stmt.close();
	      con.close();
		  %>

		</table>
	</body>   
</html>
